"""
<name>Cate-Nelson</name>
<tags>Grrr</tags>
<icon>R.png</icon>
"""
from OWRpy import * 
from libraries.base.qtWidgets.lineEdit import lineEdit as redRlineEdit 
from libraries.base.qtWidgets.radioButtons import radioButtons as redRradioButtons 
from libraries.base.qtWidgets.textEdit import textEdit as redRtextEdit 
from libraries.base.qtWidgets.commitButton import commitButton as redRCommitButton
import libraries.base.signalClasses as signals
from libraries.base.signalClasses.RVariable import RVariable as redRRVariable
from libraries.base.qtWidgets.widgetBox import widgetBox
import redREnviron, os # to be able to import CateNelson.R
import redRi18n
_ = redRi18n.get_(package = 'base')

class CateNelson(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        self.R('source(\''+unicode(os.path.join(redREnviron.directoryNames['libraryDir'], 'normality', 'widgets', 'CateNelson.R')).replace('\\', '//')+'\')')
        
        self.RFunctionParam_x = ''
        self.inputs.addInput('id0', 'Test value (one row)', redRRVariable, self.processx)
        
        area = widgetBox(self.controlArea,orientation='vertical')
        options1=widgetBox(area,orientation='horizontal')
        options2=widgetBox(area,orientation='horizontal')
        optionsCN=widgetBox(options2,orientation='vertical')
        
        self.namesListV = redRListBox(options1, label = 'Select variable')
        self.namesListV.setSelectionMode(QAbstractItemView.SingleSelection)
        self.namesListY = redRListBox(options1, label = 'Select yield')
        self.namesListY.setSelectionMode(QAbstractItemView.SingleSelection)        
        
        self.RFunctionParamxCN_lineEdit = redRlineEdit(optionsCN, label = "Cate-Nelson x:", text = '') #insert mean by default
        self.RFunctionParamyCN_lineEdit = redRlineEdit(optionsCN, label = "Cate-Nelson y:", text = '') #insert mean by default
        self.RoutputWindow = redRtextEdit(area, label = _("RoutputWindow"))
        
        self.commit = redRCommitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        
    def processx(self, data):
        if data:
            self.data = data
            self.RFunctionParam_x=data.getData()
            self.namesListV.update(self.R('names('+self.RFunctionParam_x+')', wantType = 'list'))
            self.namesListY.update(self.R('names('+self.RFunctionParam_x+')', wantType = 'list'))
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_x=''

            
    def commitFunction(self):
        if unicode(self.RFunctionParam_x) == '':
            print 'No input object'
            return
        if unicode(self.RFunctionParamxCN_lineEdit.text()) == '':
            print 'No test ariable'
            return
        if unicode(self.RFunctionParamyCN_lineEdit.text()) == '':      
            print 'No yield'
            return
        
        self.R('TestVariable<-'+unicode(self.RFunctionParam_x)+'[,'+unicode(self.namesListV.row(self.namesListV.currentItem())+1)+']')
        self.R('Yield<-'+unicode(self.RFunctionParam_x)+'[,'+unicode(self.namesListY.row(self.namesListY.currentItem())+1)+']')
        
        self.R('cnTest<-CateNelson(x=TestVariable, y=Yield, xcn='+unicode(self.RFunctionParamxCN_lineEdit.text())+', ycn='+unicode(self.RFunctionParamyCN_lineEdit.text())+')') 

        self.R('chiTest<-chisq.test(cnTest$count.table, correct=F)')
        
        self.RoutputWindow.clear()
        tmp = self.R('paste("<h3>Counts</h3>","Number of points in + +: ", cnTest$pos.count, "\nPercent of points in + +: ", sprintf("%.2f", cnTest$pos.ratio*100),"%", "<h3>Chi-squared test</h3>", "\nData name: ", chiTest$data.name, "\nMethod", chiTest$method, "\nChi-squared statistic: ", chiTest$statistic, "\nDegrees of freedom: ", chiTest$parameter, "\np-value: ", chiTest$p.value)')
        self.RoutputWindow.insertHtml('<br><pre>'+tmp+'</pre>')
        